<?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/* JoomLeague League Management and Prediction Game for Joomla!
 * @version $Id: inc.showuser.php 1126 2008-06-09 05:45:04Z julienv $
 * @package Joomleague
 * @copyright Copyright (C) 2007  JoomLeague
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see _joomleague_license.txt
 *  
 * Homepage: http://www.joomleague.de
 * Support: htt://www.joomleague.de/forum/
 * 
 * This file is part of JoomLeague.
 *  
 * JoomLeague is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Please note that the GPL states that any headers in files and
 * Copyright notices as well as credits in headers, source files
 * and output (screens, prints, etc.) can not be removed.
 * You can extend them with your own credits, though...
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
*/ 
    $user = NULL;
    
    $query = sprintf("SELECT u.id, u.username, u.name, u.registerDate, tm.fav_team, tm.champ_tip, tm.slogan, tm.reminder, tm.receipt, tm.picture, tm.last_tip as lasttip, tm.tipadmin
                        FROM #__users u
                        LEFT JOIN #__joomleague_tip_members tm ON u.id = tm.user_id
                       WHERE u.id = %s AND tm.approved = 1 AND tm.project_id = %s", $uid, $joomleague->id);
    $database->setQuery($query);
    $database->loadObject( $user );
    if (isset($user->id)) {
    $query = "SELECT r.id                  
                FROM #__joomleague_rounds r 
               WHERE r.project_id = ".$joomleague->id;
    $database->setQuery($query);
    $rounds = $database->loadResultArray();
    
    $tippsall = array();

    if ($showTipRanking_config['show_all_user']) {
      $query = sprintf("SELECT u.id
                        FROM #__users u
                        LEFT JOIN #__joomleague_tip_members tm ON u.id = tm.user_id
                       WHERE tm.approved = 1 AND tm.project_id = %s", $joomleague->id);
      $database->setQuery($query);
      $rows = $database->loadObjectList();
      foreach($rows as $row) {
        if(!array_key_exists($row->id, $tippsall)){
          $tippsall[$row->id] = new TipMachine($joomleague);
        }        
      }      
    }

    if ($r==0) {
      $thistime=$joomleague->getTimestamp('',1);
      $query = sprintf("SELECT max(matchcode)
                          FROM #__joomleague_matches m
                     LEFT JOIN #__joomleague_rounds r ON m.round_id = r.id
                         WHERE match_date <= '%s' AND
                               r.project_id = %s", date('Y-m-d H:i:s',$thistime), $joomleague->id);
      $database->setQuery($query);
      $round = $database->loadResult();
    } else $round = $r;

    $tipRanking = new TipRanking();
    $tipRanking->setProject($joomleague);
    for($i=0;$i<count($rounds);$i++) {
      if ($joomleague->overview==0) $part=0; else if ($i > intval(count($rounds)/2)-1) $part=2; else $part=1;
      $from = 1;
      if ($part==2) {
    	$from = intval(count($rounds)/2)+1;
      }

      $tipps = $tipRanking->getTippRanking('', $i+1,$i+1,0,0,'',0,$showTipRanking_config['show_all_user']);
      $ranking[$i+1]='null';
      $points[$i+1]='0';
      foreach($tipps as $key=>$val) {
        if ($val->user_id==$uid) {
          $ranking[$i+1]=$val->rank;
          $points[$i+1]=$val->points;
          break;
        }
      }
      if ($i<$round) $tippsall = $tipRanking->getTippRanking('all', $i+1, $from,0,0,'',0,$showTipRanking_config['show_all_user']);
      $rankingafterRound[$i+1]='null';
      foreach($tippsall as $key=>$val) {
        if ($val->user_id==$uid) {
          $rankingafterRound[$i+1]=$val->rank;
          break;
        }
      }
    }
    foreach($tippsall as $key=>$val) {
      if ($val->user_id==$uid) {
        $rankingall = $val->rank;
        $pointsall = $val->points;
        $tippCount = $val->tippCount;
        $hitsCount = $val->hitsCount;
        $diffCount = $val->diffCount;
        $tendCount = $val->tendCount;
        break;
      }
    }
    unset($tippsall);
    $tippRanking = new TipRanking();
    $tippRanking->setProject($joomleague);
    $lasttipps = $tippRanking->getTippRanking('',$round,$round,0,0,'',$uid);
    if (isset($lasttipps[0])) $lasttipp = $lasttipps[0]->points;
    unset($lasttipps);
  
    $database->setQuery(sprintf("SELECT t.name AS name
                                   FROM #__joomleague_team_joomleague tl
                              LEFT JOIN #__joomleague_teams t on tl.team_id = t.id
                                  WHERE tl.project_id = %s AND tl.id = %s", $joomleague->id, $user->champ_tip));
    $champ_tip = $database->loadResult();
    
    $database->setQuery(sprintf("SELECT t.name AS name
                                   FROM #__joomleague_team_joomleague tl
                              LEFT JOIN #__joomleague_teams t on tl.team_id = t.id
                                  WHERE tl.project_id = %s AND tl.id = %s", $joomleague->id, $user->fav_team));
    $fav_team = $database->loadResult();

    $tipteams[] = mosHTML::makeOption( '0', _JL_USER_PAGE_TEAM );    
    $database->setQuery(sprintf("SELECT tl.id AS value, t.name AS text
                                   FROM #__joomleague_team_joomleague tl
                              LEFT JOIN #__joomleague_teams t on tl.team_id = t.id
                                  WHERE tl.project_id = %s ORDER BY t.name",$joomleague->id));
    $tipteams = array_merge( $tipteams, $database->loadObjectList() );
    
    $clist = mosHTML::selectList( $tipteams, 'fav_team', 'class="inputbox" size="1"','value', 'text', $user->fav_team );    
    
    $spr = round((count($tipteams)-2)/2);
    if ($joomleague->tip_joker) {
        $maxpunkte = $joomleague->tip_points_correct_result_joker + (($spr-1)*$joomleague->tip_points_correct_result);
    } else {
        $maxpunkte = $spr*$joomleague->tip_points_correct_result;
    }
    
    $tlist = mosHTML::selectList( $tipteams, 'champ_tip', 'class="inputbox" size="1"','value', 'text', $user->champ_tip ); 

    $query = "SELECT m.match_date as match_date 
                FROM #__joomleague_matches m
               WHERE m.project_id = ".$joomleague->id. " 
               AND m.match_date > 0
            ORDER BY m.match_date LIMIT 1"; 
    $database->setQuery($query);
    $saisonstart = $joomleague->getTimestamp($database->loadResult());

    $database->setQuery( "SELECT tm.user_id AS value, u.username AS text FROM #__joomleague_tip_members tm LEFT JOIN #__users u ON tm.user_id = u.id WHERE approved=1 AND tm.project_id = $joomleague->id ORDER BY u.username");
    $userselect = $database->loadObjectList();

    $pageNav2=<<<EOL
    <form action="" method="get" style="display:inline;">
    <select class="inputbox" onchange="joomleague_changedoc(this)">
EOL;
    
    $url = JL_COMPONENT_LINK."&amp;func=".$jl_func."&amp;p=".$joomleague->id.JL_ITEMID_SUFFIX;
    foreach($userselect as $ulist) {
       $pageNav2.="<option value='";
       $pageNav2.=sefRelToAbs($url.'&amp;uid='.$ulist->value)."'";
       if ($ulist->value==$uid) { $pageNav2.=" selected='selected'"; }
       $pageNav2.=">";
       $pageNav2.="$ulist->text</option>";
    }
    $pageNav2.=<<<EOL
    </select></form>
EOL;
$filedir = (isset($showUser_config['picture_dir'])) ? $showUser_config['picture_dir'] :'images/joomleague/users';
    if ($user->picture != '' && !is_null($user->picture) && file_exists(_JOOMLA_JL_PATH.'/'.$filedir.'/'.$user->picture)&&$user->picture){
      $user_picture=_JOOMLA_JL_LIVE_SITE.'/'.$filedir.'/'.$user->picture;
    }
    else $user_picture=_JOOMLA_JL_LIVE_SITE.'/images/placeholders/placeholder_150.png';
    }
    if ($mytask=="edit" && $allowed==1) {
      require(getTemplateFile(JL_TEMPLATE_PATH, 'edit_user.tpl.php'));
    } else if (isset($user->username) && $user->username) require(getTemplateFile(JL_TEMPLATE_PATH, 'show_user.tpl.php'));
    else print 'no data!';
// Tippspiel Benutzer Profil ENDE
//##########################################################################################################################